//
//  NSDate+ILGLib.m
//  ILGDemo
//
//  Created by zhangtongle-Pro on 2017/9/6.
//  Copyright © 2017年 Happy. All rights reserved.
//

#import "NSDate+ILGLib.h"

@implementation NSDate (ILGLib)

+ (NSString *)ilg_timeStamp {
    NSString *timeStamp = [NSString stringWithFormat:@"%ld", (long)([[self date] timeIntervalSince1970])];
    
    return timeStamp;
}

+ (NSString *)ilg_timeZone {
    /*
     timeZone.abbreviation GMT+8 or GMT-8
     */
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    
    return [timeZone.abbreviation substringFromIndex:3];
}

+ (NSDate *)ilg_localDate {
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:date];
    NSDate *localDate = [date dateByAddingTimeInterval:interval];
    
    return localDate;
}

+ (NSString *)ilg_localDateStr:(NSString *)formatStr {
    NSDate *GTMDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    formatStr?[dateFormatter setDateFormat:formatStr]:[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    return [dateFormatter stringFromDate:GTMDate];
}

- (NSString *)ilg_dateStr:(NSString *)formatStr {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    formatStr?[dateFormatter setDateFormat:formatStr]:[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    return [dateFormatter stringFromDate:self];
}

+ (double)ilg_timeIntervalBetween:(NSDate *)firstDate date:(NSDate *)secondDate {
    double timeInterval = [firstDate timeIntervalSinceDate:secondDate];
    
    return timeInterval;
}

+ (NSString *)ilg_fileBuildTime {
    NSString *buildTimeStr = [NSString stringWithFormat:@"%s %s",__DATE__, __TIME__];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    dateFormatter.dateFormat = @"MM dd yyyy HH:mm:ss";
    NSDate *date = [dateFormatter dateFromString:buildTimeStr];
    
    NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
    [dateFormatter2 setTimeZone:[NSTimeZone localTimeZone]];
    [dateFormatter2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    return [dateFormatter2 stringFromDate:date];
}

@end

@implementation NSString (NSDate)
- (NSDate *)ilg_dateWithFormat:(NSString *)formatStr {
    if (formatStr.length == 0) {
        return nil;
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    [dateFormatter setTimeZone:timeZone];
    formatStr?[dateFormatter setDateFormat:formatStr]:[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    return [dateFormatter dateFromString:self];
}

- (NSString *)ilg_timeWithFormat:(NSString *)format {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    dateFormatter.timeStyle = NSDateFormatterShortStyle;
    [dateFormatter setDateFormat:format];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.doubleValue];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    
    return dateStr;
}
@end
